package com.myapp.ruyixing.http;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.myapp.ruyixing.model.CityHotMenu;

public class CityHotDistrictParser extends RYJSONParser {

	@Override
	public Object parse(JSONObject jsonObject) {
		if (getErrno(jsonObject) != NO_ERROR) {
			return null;
		}

		CityHotMenu cityHot = new CityHotMenu();
		cityHot.labels = new ArrayList<CityHotMenu.CityHotLabel>();

		JSONArray data = jsonObject.optJSONArray("data");

		for (int i = 0; i < data.length(); i++) {
			JSONObject label = data.optJSONObject(i);

			if (label != null) {
				String sLabel = label.optString("label");
				JSONArray cells = label.optJSONArray("districts");

				CityHotMenu.CityHotLabel hotLabel = new CityHotMenu.CityHotLabel();
				hotLabel.label = sLabel;
				hotLabel.cells = new ArrayList<CityHotMenu.HotCell>();
				
				if (cells != null) {
					for (int j = 0; j < cells.length(); j++) {
						JSONObject cell = cells.optJSONObject(j);
						
						if (cell != null) {
							CityHotMenu.HotCell hotCell = new CityHotMenu.HotCell();
							hotCell.id = cell.optString("id");
							hotCell.name = cell.optString("name");
							
							hotLabel.cells.add(hotCell);
						}
					}
				}
				
				cityHot.labels.add(hotLabel);
			}
		}
		
		return cityHot;
	}
}
